/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.asset.bean;

import com.yunhoo.asset.db.impl.AssetSqlImpl;
import com.yunhoo.asset.model.Asset;
import com.yunhoo.asset.model.AssetExtend;
import com.yunhoo.base.model.Department;
import com.yunhoo.base.model.Person;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.CommonParam;
import com.yunhoo.sys.util.common.FacesUtils;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.Null;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem; 

/**
 *
 * @author Administrator
 */ 
@ManagedBean(name = "assetEditBean")
@SessionScoped
public class AssetEditBean {

    private final String NAMESPACE = "psncard.";
    private Asset asset = new Asset();
    private Asset condition = new Asset();
    private List<Asset> assetlist = new ArrayList<Asset>();
    private DataModel assetmodel = new ListDataModel();
    private Department noAuditedDept = new Department();
    private boolean addOrUpdate = false;
    private boolean editStatus = false;
    private List<Department> deptList = new ArrayList();
    private DeptAndEmpHelpBean deptAndEmp = (DeptAndEmpHelpBean) FacesUtils.getManagedBean("deptAndEmp");
    private String deptCode = "";
    private List<SelectItem> deptSelectli = null;
    private List<SelectItem> personSelectli = null;
    private DataModel<AssetExtend> cateModel = new ListDataModel<AssetExtend>();//采购执行列表,用于生产资产的参照
    private List<Asset> assetFromProcure = null;//采购生成资产；
    private String fromProcure;//是否来自政采

    public AssetEditBean() {
        initDeptSelectli();
    }

    private void initDeptSelectli() {
        deptSelectli = new ArrayList<SelectItem>();
        deptList = CommonParam.departmentList();
        for (Department d : deptList) {
            deptSelectli.add(new SelectItem(d.getCode(), d.getName()));
        }
    }

    public void initStatus() {
        addOrUpdate = false;
        editStatus = false;
    }

    private List<Person> getPersonList(String deptCode) {
        Person param = new Person();
        param.setOrgCode(LoginParam.getOrg().getCode());
        param.setIyear(LoginParam.getIyear());
        param.setDeptCode(deptCode);
        return IbatisTemplate.queryForList("person.queryListByDept", param);
    }

    public void deptChange() {
        /*
         * 当前行 部门 --> 人员列表 --> 选择使用人
         */
        System.out.println("deptCode " + deptCode);
        if (deptCode == null || deptCode.isEmpty()) {
            return;
        }
        asset.getDept().setCode(deptCode);
        List<Person> personlist = getPersonList(deptCode);
        if (personlist != null && personlist.size() > 0) {
            personSelectli = new ArrayList<SelectItem>();
            for (Person p : personlist) {
                personSelectli.add(new SelectItem(p.getCpsn_number(), p.getCpsn_name()));
            }
        }
    }

    public void queryAsset() {
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("iyear", LoginParam.getIyear());
        map.put("deptcode", condition.getDept().getCode());
        map.put("cpsn_name", condition.getPerson().getCpsn_name());
        map.put("assetName", condition.getAssetName());
        map.put("priceB", condition.getPriceB());
        map.put("priceE", condition.getPriceE());
        map.put("purchDateB", condition.getPurchDateB());
        map.put("purchDateE", condition.getPurchDateE());
        map.put("shelfLifeB", condition.getShelfLifeB());
        map.put("shelfLifeE", condition.getShelfLifeE());
        map.put("fromProcure", fromProcure);
        map.put("checkFlag", 0);
        assetlist = IbatisTemplate.queryForList(NAMESPACE + "queryForAsset", map);
        assetmodel.setWrappedData(assetlist);
    }

    public void add() {
        addOrUpdate = true;
        editStatus = true;
        asset = new Asset();
        asset.setAssetCode(maxCode());
        asset.setOrgCode(LoginParam.getOrg().getCode());
    }

    public void update() {
        addOrUpdate = false;
        editStatus = true;
    }

    public void save() {
        asset.setOrgCode(LoginParam.getOrg().getCode());
        if (addOrUpdate) {
            IbatisTemplate.insert(NAMESPACE + "insertAsset", asset);
        }
        if (!addOrUpdate) {
            IbatisTemplate.update(NAMESPACE + "updateAsset", asset);
        }
        initStatus();
        queryAsset();
    }

    public void delete() {
        asset.setOrgCode(LoginParam.getOrg().getCode());
        IbatisTemplate.delete(NAMESPACE + "deleteAsset", asset);
        initStatus();
        queryAsset();
    }

    public void cancel() {
        if (addOrUpdate) {
            asset = new Asset();
        }
        initStatus();
    }

    public void show() {
        asset = (Asset) assetmodel.getRowData();
        deptAndEmp.buildPsnList(asset.getDept().getCode());
    }

    public String maxCode() {
        String orgCode = LoginParam.getOrg().getCode();
        Integer maxCode = (Integer) IbatisTemplate.query(NAMESPACE + "queryMaxAssetCode", orgCode);
        if (maxCode == null) {
            maxCode = 0;
        }
        return String.format("%08d%n", maxCode + 1).trim();
    }

    public Asset getAsset() {
        return asset;
    }

    public void setAsset(Asset asset) {
        this.asset = asset;
    }

    public Asset getCondition() {
        return condition;
    }

    public void setCondition(Asset condition) {
        this.condition = condition;
    }

    public List<Asset> getAssetlist() {
        return assetlist;
    }

    public void setAssetlist(List<Asset> assetlist) {
        this.assetlist = assetlist;
    }

    public DataModel getAssetmodel() {
        return assetmodel;
    }

    public void setAssetmodel(DataModel assetmodel) {
        this.assetmodel = assetmodel;
    }

    public Department getNoAuditedDept() {
        return noAuditedDept;
    }

    public void setNoAuditedDept(Department noAuditedDept) {
        this.noAuditedDept = noAuditedDept;
    }

    public boolean isAddOrUpdate() {
        return addOrUpdate;
    }

    public void setAddOrUpdate(boolean addOrUpdate) {
        this.addOrUpdate = addOrUpdate;
    }

    public boolean isEditStatus() {
        return editStatus;
    }

    public void setEditStatus(boolean editStatus) {
        this.editStatus = editStatus;
    }

    public List<Department> getDeptList() {
        return deptList;
    }

    public void setDeptList(List<Department> deptList) {
        this.deptList = deptList;
    }

    public DeptAndEmpHelpBean getDeptAndEmp() {
        return deptAndEmp;
    }

    public void setDeptAndEmp(DeptAndEmpHelpBean deptAndEmp) {
        this.deptAndEmp = deptAndEmp;
    }

    public String getDeptCode() {
        return deptCode;
    }

    public void setDeptCode(String deptCode) {
        this.deptCode = deptCode;
    }

    public List<SelectItem> getDeptSelectli() {
        return deptSelectli;
    }

    public void setDeptSelectli(List<SelectItem> deptSelectli) {
        this.deptSelectli = deptSelectli;
    }

    public List<SelectItem> getPersonSelectli() {
        return personSelectli;
    }

    public void setPersonSelectli(List<SelectItem> personSelectli) {
        this.personSelectli = personSelectli;
    }

    public DataModel<AssetExtend> getCateModel() {
        return cateModel;
    }

    public void setCateModel(DataModel<AssetExtend> cateModel) {
        this.cateModel = cateModel;
    }

    public List<Asset> getAssetFromProcure() {
        return assetFromProcure;
    }

    public void setAssetFromProcure(List<Asset> assetFromProcure) {
        this.assetFromProcure = assetFromProcure;
    }

    public String getFromProcure() {
        return fromProcure;
    }

    public void setFromProcure(String fromProcure) {
        this.fromProcure = fromProcure;
    }
    
}
